﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using FriendsNearby.Services;
using Twitter;
using System.Net;
using System.Configuration;
using Microsoft.Practices.EnterpriseLibrary.Logging;

namespace NearbyNotifications
{
    class Program
    {
        static void Main(string[] args)
        {
            string botUser = ConfigurationManager.AppSettings["botUser"];
            string botPassword = ConfigurationManager.AppSettings["botPassword"];

            if(string.IsNullOrEmpty(botUser) || string.IsNullOrEmpty(botPassword))
            {
                Console.Out.WriteLine("Could not find botUser or botPassword in configuration");
                return;
            }

            NetworkCredential credential = new NetworkCredential(botUser, botPassword);

            IBotClient twitterClient = new BotClient(credential);
            				
            FriendsNearby.Services.FriendsNearbySystem facade = new FriendsNearby.Services.FriendsNearbySystem();

            DataAccess data = new DataAccess();
            IEnumerable<string> botFriends = twitterClient.Friendship.GetFriends();
            IEnumerable<string> allTwitterId = data.GetAllTwitterUserIds();

            foreach (string twitterUserId in allTwitterId.Intersect<string>(botFriends))
            {
                if (!string.IsNullOrEmpty(twitterUserId))
                {
                    try
                    {
                        string notificationMessage = String.Empty;
						IUsersService userService = facade.GetService<IUsersService>();
						UserRegistry user = userService.QueryUserRegistry(x=>x.TwitterScreenName == twitterUserId);

						if (user == null)
                        {
                            //notificationMessage = "You are not getting user nearby notifications because your twitter"
                            //                        + " account is not linked with a facebook account";
                        }
                        else
                        {
                            var notifications = facade.GetService<IPeopleNearbyService>().GetNotificationsForUser(user.UserID);

                            // TODO: the format of the message should be the same of the nearby processor, the difference is that
                            // the message in case notifications is empty, here is "". 
                            foreach (var notification in notifications)
                            {
                                notificationMessage += string.Format("{0} is now nearby" + Environment.NewLine, notification);
                            }
                        }
                        if (!String.IsNullOrEmpty(notificationMessage))
                        {
                            twitterClient.Messages.Send(twitterUserId, notificationMessage);
                            TwitterMessageLogger.AddLog(null, notificationMessage, "NearbyNotifications", null, twitterUserId);
                        }
                        facade.GetService<ILocationService>().SaveLastNotificationDate(user.UserID, DateTime.Now);
                    }
                    catch (Exception ex)
                    {
                        TwitterMessageLogger.AddLog(null, null, "NearbyNotifications", ex, twitterUserId);
                        Logger.Write(string.Format(" Exception thrown in NearbyNotifications:{0}, trying to send a notification message to {1}",
                                            ex.ToString(), twitterUserId));                        
                    }
                    

                }
            }

        }
    }
}
